import React, {Component} from 'react';
import LinkButton from "../Public/LinkButton";
import ProductCategoryTable from './ProductCategoryTable';

import http from '../../http'

class Index extends Component {
	constructor(props) {
		super(props);
		this.state = {
			list: []
		}
	}


	componentDidMount() {
		this.getProductCategoryList(this.props)
	}

	componentWillUpdate(nextProps) {
		if (this.props.match.params.id !== nextProps.match.params.id) {
			this.getProductCategoryList(nextProps);
		}
	}

	getProductCategoryList(props) {
		let id = props.match.params.id;
		this.setState({
			list: []
		});
		http.getProductCategoryList(id).then(res => {
			if (res.status === 0) {
				this.setState({
					list: res.data
				})
			} else if (res.status === 10) {
				alert(res.msg);
				this.props.history.push('/login');
			}

		})
	}

	render() {
		return (
			<div>
				<div className="home-title">
					<span>品类管理</span>
					<LinkButton to="/product-category/add">添加品类</LinkButton>
				</div>
				<div>
					当前商品分类的ID：<span>{this.props.match.params.id || 0}</span>
				</div>
				<ProductCategoryTable list={this.state.list}/>
			</div>
		);
	}
}

export default Index;